<template>
  <div class="edit-container">
    <!-- 导航条 -->
    <van-nav-bar
      left-arrow
      title="编辑资料"
      @click-left="onClickLeft"
    ></van-nav-bar>
    <!-- 头像部分 -->
    <div class="avatar">
      <!-- 图片 -->
      <van-image fit="cover" round :src="userInfo.photo" />
      <!-- 图片上传组件 -->
      <van-uploader :after-read="afterRead" />
    </div>
    <!-- 信息展示 -->
    <van-cell-group>
      <van-cell
        is-link
        title="名称"
        :value="userInfo.name"
        @click="showeditname"
      />
      <van-cell
        is-link
        title="性别"
        @click="showgender = true"
        :value="userInfo.gender === 1 ? '女' : '男'"
      />
      <van-cell
        is-link
        title="生日"
        :value="userInfo.birthday"
        @click="showBirthdayPop"
      />
    </van-cell-group>
    <!-- 修改昵称弹框 -->
    <van-dialog
      @confirm="saveNameEdit"
      v-model="show"
      title="修改昵称"
      show-cancel-button
    >
      <van-field ref="namefocus" v-model="name" placeholder="请输入用户名" />
    </van-dialog>
    <!-- 修改性别弹框 -->
    <van-popup v-model="showgender" position="bottom">
      <van-nav-bar
        title="修改性别"
        left-text="取消"
        @click-left="showgender = false"
      />
      <van-cell-group>
        <van-cell title="男" is-link @click="savegender(0)" />
        <van-cell title="女" is-link @click="savegender(1)" />
      </van-cell-group>
    </van-popup>
    <!-- 生日编辑弹框 -->
    <van-popup v-model="showBirthday" position="bottom">
      <van-datetime-picker
        @cancel="showBirthday = false"
        v-model="birthday"
        type="date"
        title="选择年月日"
        :min-date="minDate"
        @confirm="saveBirthday"
      />
    </van-popup>
    <div class="cropper-container" v-show="showCropper">
      <vueCropper
        ref="cropper"
        :img="img"
        autoCrop
        autoCropHeight="120"
        autoCropWidth="120"
      ></vueCropper>
      <van-button type="primary" class="confirm-btn" @click="confirmCropper"
        >确认</van-button
      >
      <van-button type="primary" class="cancel-btn" @click="showCropper = false"
        >取消</van-button
      >
    </div>
  </div>
</template>

<script>
import { mapState } from 'vuex'
// 导入修改用户信息API
import { editUserInfo, uploadPhoto } from '../../../api/edit'
// 导入处理时间的moment
import moment from 'moment'
// 导入截图组件
import { VueCropper } from 'vue-cropper'
export default {
  name: 'editUser',
  data () {
    return {
      // 截图组件用的图片
      img: '',
      // 控制显示截图界面
      showCropper: false,
      // 控制弹窗的显示
      show: false,
      // 弹窗中显示绑定的用户名字
      name: '',
      // 控制性别弹窗
      showgender: false,
      // 控制生日弹框
      showBirthday: false,
      // 增加日期字段
      birthday: '',
      // 最小日期
      minDate: new Date(1980, 0, 1)
    }
  },
  computed: mapState(['userInfo']),
  // computed: {
  //   userinfo () {
  //     return this.$store.state.userInfo
  //   }
  // },
  components: {
    VueCropper
  },
  methods: {
    // 保存用户的生日
    saveBirthday () {
      const birthday = moment(this.birthday).format('YYYY-MM-DD')
      editUserInfo({ birthday }).then(res => {
        console.log(res)
        this.showBirthday = false
        this.$store.commit('setUserInfo', {
          ...this.userInfo,
          birthday
        })
        this.$toast.success('修改生日成功')
      })
    },
    // 保存姓名修改
    saveNameEdit () {
      editUserInfo({ name: this.name }).then(res => {
        this.$store.commit('setUserInfo', {
          ...this.userInfo,
          name: this.name
        })
        this.$toast.success('修改姓名成功')
      })
    },
    // 保存性别修改
    savegender (gender) {
      editUserInfo({ gender }).then(res => {
        this.showgender = false
        this.$store.commit('setUserInfo', {
          ...this.userInfo,
          gender
        })
        this.$toast.success('修改性别成功')
      })
    },
    // 点击显示生日弹框
    showBirthdayPop () {
      this.showBirthday = true
      this.birthday = new Date(this.userInfo.birthday)
    },
    // 点击显示名字
    showeditname () {
      this.show = true
      this.name = this.userInfo.name
      // 获取焦点，ref的focus直接写在方法里是显示不出来的
      // 所以要通过一个$$nextTick注册一个回调函数，可以在DOM更新完毕之后执行
      this.$nextTick(() => {
        this.$refs.namefocus.focus()
      })
    },
    // 点击返回
    onClickLeft () {
      // this.$router.go(-1)
      this.$router.back()
    },
    afterRead (file) {
      // 此时可以自行将文件上传至服务器
      // console.log(file)
      // const formData = new FormData()
      // formData.append('photo', file.file)
      // uploadPhoto(formData).then(res => {
      //   console.log(res)
      // this.$store.commit('setUserInfo', {
      //   ...this.userInfo,
      //   photo: res.data.data.photo
      // })
      // })
      this.showCropper = true
      this.img = file.content
    },
    // 点击确定后更换头像
    confirmCropper () {
      this.$refs.cropper.getCropBlob(data => {
        console.log(data)
        const formdata = new FormData()
        formdata.append('photo', data)
        uploadPhoto(formdata).then(res => {
          this.$store.commit('setUserInfo', {
            ...this.userInfo,
            photo: res.data.data.photo
          })
          this.showCropper = false
        })
      })
    }
  }
}
</script>

<style lang="less">
.edit-container {
  // 导航条部分
  .van-nav-bar {
    background-color: #3196fa;
    .van-nav-bar__title {
      color: #fff;
    }
    .van-icon-arrow-left {
      color: #fff;
    }
  }
  // 头像部分
  .avatar {
    padding: 20px 0;
    text-align: center;
    .van-image {
      width: 120px;
      height: 120px;
    }
    position: relative;
    .van-uploader {
      position: absolute;
      opacity: 0;
      top: 20px;
      left: 50%;
      transform: translateX(-50%);
      width: 120px;
      height: 120px;
      .van-uploader__upload {
        width: 120px;
        height: 120px;
      }
    }
  }
  .van-dialog__content {
    padding: 10px;
  }
  .van-field {
    border: 1px solid #ccc;
  }
  .van-nav-bar__text {
    color: white;
  }
  // 截图组件样式
  // 截图组件样式
  .cropper-container {
    position: fixed;
    left: 0;
    top: 0;
    right: 0;
    bottom: 0;
    .van-button {
      position: absolute;
      bottom: 0;
    }
    .confirm-btn {
      left: 0;
    }
    .cancel-btn {
      right: 0;
    }
  }
}
</style>
